//
//  MapManager.m
//  FrameWork
//
//  Created by Eagle on 5/27/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MapManager.h"

@implementation Location

@synthesize x = _x;
@synthesize y = _y;

+(id) LocationMake: (int)x y:(int)y
{
	Location* loc = [[Location alloc] init];
	loc.x = x;
	loc.y = y;
	return loc;
}

-(id) init
{
	_x = 0;
	_y = 0;
	return self;
}

-(void) dealloc
{
	[super dealloc];
}

@end



@implementation MapManager

-(id)init
{
	_start = nil;
	_end = nil;
	
	_mapLoc = [[NSMutableArray alloc] init];
	_backMapLoc = [[NSMutableArray alloc] init];
	_openList = [[NSMutableArray alloc] init];
	_closeList = [[NSMutableArray alloc] init];
	_preOpenList = [[NSMutableArray alloc] init];
	
	
	//武将移动地图
	_warriorMapLoc = [[NSMutableArray alloc] init];
	
	
	//武将判断用地图
	_backWarriorMapLoc = [[NSMutableArray alloc] init];
	
	
	_row = 0;
	_column = 0;
	
	return self;
}

-(void) LoadMap: (const char *) filename type:(int)type
{
	
	_type = type;
	CReader reader;
	reader.OpenResource(filename);
	int nRow = 0;
	int nColume = 0;
	int nTemp = 0;
	
	reader.ReadInt(&nColume);
	reader.ReadInt(&nRow);
	_row = nRow;
	_column = nColume;
	int i = 0;
	int j = 0;
	for(i = 0; i<nRow; i++)
	{
		NSMutableArray *array = [[NSMutableArray alloc] init];
		NSMutableArray *backarray = [[NSMutableArray alloc] init];
		
		NSMutableArray *warriorarray = [[NSMutableArray alloc] init];
		NSMutableArray *backwarriorarray = [[NSMutableArray alloc] init];
		
		
		for(j = 0; j < nColume; j++)
		{
			reader.ReadInt(&nTemp);
			
			if(1 == _type)
			{
				if (MAPFILE_END == (nTemp + 1)) 
				{
					
					
					NSNumber* num = [NSNumber numberWithInt:MAP_END];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_END];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
					
					_end = [Location LocationMake:j y:i];
					
				}
				else if(MAPFILE_START == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_DEFAULT];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_DEFAULT];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
					
					_start = [Location LocationMake:j y:i];
				}
				else if(MAPFILE_NOTOWER == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_NOTOWER];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_NOTOWER];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_NOTOWER];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_NOTOWER];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
					
				}
				else if(MAPFILE_TOWER == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_DEFAULT];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_DEFAULT];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
					
				}
				else if(MAPFILE_PATH == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_DEFAULT];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_DEFAULT];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
				}
				else
				{
					printf("Map File Error!\n");
				}
			}
			
			else if( 0 == _type)
			{
				if (MAPFILE_END == (nTemp + 1)) 
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_END];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_END];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_PATH];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_PATH];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
					
					
					_end = [Location LocationMake:j y:i];
				}
				else if(MAPFILE_START == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_PATH];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_PATH];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_PATH];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_PATH];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
					
					_start = [Location LocationMake:j y:i];
				}
				else if(MAPFILE_NOTOWER == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_NOTOWER];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_NOTOWER];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_NOTOWER];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_NOTOWER];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
					
				}
				else if(MAPFILE_TOWER == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_TOWER];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_TOWER];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_TOWER];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_TOWER];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
				}
				else if(MAPFILE_PATH == (nTemp + 1))
				{
					NSNumber* num = [NSNumber numberWithInt:MAP_PATH];
					NSNumber* backnum = [NSNumber numberWithInt:MAP_PATH];
					[array addObject:num];
					[backarray addObject:backnum];
					
					NSNumber* warriorNum = [NSNumber numberWithInt:MAP_PATH];
					NSNumber* backWarriorNum = [NSNumber numberWithInt:MAP_PATH];
					[warriorarray addObject:warriorNum];
					[backwarriorarray addObject:backWarriorNum];
				}
				else
				{
					printf("Map File Error!\n");
				}
			}
			
		}
		[_mapLoc addObject:array];
		[_backMapLoc addObject:backarray];
		[_warriorMapLoc addObject:warriorarray];
		[_backWarriorMapLoc addObject:backwarriorarray];
		
		[array release];
		[backarray release];
		[warriorarray release];
		[backwarriorarray release];
	}
	
	reader.Close();
}

-(void) MarkFullMap:(Location*) loc
{
	if (0 == _type) 
	{
		[_openList removeAllObjects];
		[_closeList removeAllObjects];
		[_preOpenList removeAllObjects];
		[_openList addObject:loc];
		
		unsigned int i = 0;
		unsigned int j = 0;
		
		while ([_openList count] > 0) 
		{
			[_preOpenList removeAllObjects];
			for(i = 0; i < [_openList count]; i++)
			{
				int nTemp = 0;
				Location *location = [_openList objectAtIndex:i];
				int nValue = [[[_backMapLoc objectAtIndex:location.y] objectAtIndex:location.x] intValue];
				
				//左边
				nTemp = location.x - 1;
				if(nTemp >= 0 && MAP_PATH == [[[_backMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
				{
					
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
					
					Location* aLoc = [Location LocationMake:nTemp y:location.y];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					continue;
				}
				
				
				//NSInteger 
				//右边
				nTemp = location.x + 1;
				if(nTemp < _column && MAP_PATH == [[[_backMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
					
					Location* aLoc = [Location LocationMake:nTemp y:location.y];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					continue;
				}
				
				//上边
				nTemp = location.y - 1;
				if(nTemp >= 0 && MAP_PATH == [[[_backMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
					
					Location* aLoc = [Location LocationMake:location.x y:nTemp];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					continue;
				}
				
				//下边
				nTemp = location.y + 1;
				if(nTemp < _row && MAP_PATH == [[[_backMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
					
					Location* aLoc = [Location LocationMake:location.x y:nTemp];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					continue;
				}
				
			}
			
			[_openList removeAllObjects];
			for(Location* aLoc in _preOpenList)
			{
				[_openList addObject:aLoc];
			}
		}
		
		
		//for(i = 0; i < _row; i++)
//		{
//			for(j = 0; j < _column; j++)
//			{
//				printf("%d ,\t", [[[_backMapLoc objectAtIndex:i] objectAtIndex:j] intValue]);
//			}
//			printf("\n");
//		}
		
		
		return;
	}
	[_openList removeAllObjects];
	[_closeList removeAllObjects];
	[_preOpenList removeAllObjects];
	[_openList addObject:loc];
	
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < _row; i++)
	{
		for(j = 0; j < _column; j++)
		{
			int nTemp = [[[_backMapLoc objectAtIndex:i] objectAtIndex:j] intValue];
			if (nTemp != MAP_NOTOWER && nTemp != MAP_WITHTOWER && nTemp!= MAP_END)
			{
				NSNumber* num = [NSNumber numberWithInt:MAP_DEFAULT];
				[[_backMapLoc objectAtIndex:i] replaceObjectAtIndex:j withObject:num];
				
				
			}
		}
	}
	
	
	while ([_openList count] > 0) 
	{
		[_preOpenList removeAllObjects];
		for(i = 0; i < [_openList count]; i++)
		{
			int nTemp = 0;
			
			
			Location *location = [_openList objectAtIndex:i];
			int nValue = [[[_backMapLoc objectAtIndex:location.y] objectAtIndex:location.x] intValue];
			
			//左边
			nTemp = location.x - 1;
			if(nTemp >= 0 && MAP_DEFAULT == [[[_backMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
			{
				
				NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
				[[_backMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
				
				Location* aLoc = [Location LocationMake:nTemp y:location.y];
				[_preOpenList addObject:aLoc];
				[aLoc release];
				
			}
			
			
			//NSInteger 
			//右边
			nTemp = location.x + 1;
			if(nTemp < _column && MAP_DEFAULT == [[[_backMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
			{
				NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
				[[_backMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
				
				Location* aLoc = [Location LocationMake:nTemp y:location.y];
				[_preOpenList addObject:aLoc];
				[aLoc release];
				
			}
			
			//上边
			nTemp = location.y - 1;
			if(nTemp >= 0 && MAP_DEFAULT == [[[_backMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
			{
				NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
				[[_backMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
				
				Location* aLoc = [Location LocationMake:location.x y:nTemp];
				[_preOpenList addObject:aLoc];
				[aLoc release];
			}
			
			//下边
			nTemp = location.y + 1;
			if(nTemp < _row && MAP_DEFAULT == [[[_backMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
			{
				NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
				[[_backMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
				
				Location* aLoc = [Location LocationMake:location.x y:nTemp];
				[_preOpenList addObject:aLoc];
				[aLoc release];
			}
			
		}
		
		[_openList removeAllObjects];
		for(Location* aLoc in _preOpenList)
		{
			[_openList addObject:aLoc];
		}
	}

}

-(BOOL) EnableTower:(Location*) loc
{
	if(0 == _type)
	{
		if(MAP_TOWER == [[[_mapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue])
		{
			return YES;
		}
		else 
		{
			return NO;
		}

	}
	
	if([[[_backMapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue] > 0)
	{
		return YES;
	}
	else 
	{
		return NO;
	}

	
	
}
-(void) PutTower:(Location*) loc
{
	NSNumber *num = [NSNumber numberWithInt:MAP_WITHTOWER];
	[[_mapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:num];
	
	NSNumber *warriorNum = [NSNumber numberWithInt:MAP_WITHTOWER];
	[[_warriorMapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:warriorNum];
	
}

-(void) PutBackTower:(Location*) loc
{
	NSNumber *num = [NSNumber numberWithInt:MAP_WITHTOWER];
	[[_backMapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:num];
	
	NSNumber *warriorNum = [NSNumber numberWithInt:MAP_WITHTOWER];
	[[_backWarriorMapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:warriorNum];
}

-(void) RemoveTower:(Location*) loc
{
	if(0 == _type)
	{
		if (MAP_WITHTOWER == [[[_mapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue]) 
		{
			NSNumber *num = [NSNumber numberWithInt:MAP_TOWER];
			[[_mapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:num];
		}
	}
	
	else if(1 == _type)
	{
		NSNumber *num = [NSNumber numberWithInt:MAP_DEFAULT];
		[[_mapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:num];
		
		NSNumber *warriorNum = [NSNumber numberWithInt:MAP_DEFAULT];
		[[_warriorMapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:warriorNum];
		
		
		
		[self ResetBack];
		[self MarkFullMap:_end];
		[self CopyBack];
		
	}
	
}

-(void) CopyBack
{
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < _row; i++)
	{
		for(j = 0; j < _column; j++)
		{
			int nTemp = 0;
			nTemp = [[[_backMapLoc objectAtIndex:i] objectAtIndex:j] intValue];
			NSNumber *num = [NSNumber numberWithInt:nTemp];
			
			[[_mapLoc objectAtIndex:i] replaceObjectAtIndex:j withObject:num]; 
		}
	}
	
}

-(void) ResetBack
{
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < _row; i++)
	{
		for(j = 0; j < _column; j++)
		{
			int nTemp = 0;
			nTemp = [[[_mapLoc objectAtIndex:i] objectAtIndex:j] intValue];
			NSNumber *num = [NSNumber numberWithInt:nTemp];
			
			[[_backMapLoc objectAtIndex:i] replaceObjectAtIndex:j withObject:num]; 
		}
	}

}

-(int) GetType
{
	return _type;
}

-(Location*) GetStart
{
	return _start;
}
-(Location*) GetEnd
{
	return _end;
}

-(int) GetNumber:(Location*) loc
{
	return [[[_mapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
}

-(int) GetWarriorBackNumber:(Location*) loc
{
	return  [[[_backWarriorMapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
}

-(Location*) GetNextLocation:(Location*) loc
{
	int num = [[[_mapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
	int nTemp = 0;
	
	//右边
	nTemp = loc.x + 1;
	if (nTemp < _column && (num - 1) == [[[_mapLoc objectAtIndex:loc.y] objectAtIndex:nTemp] intValue]) 
	{
		return [Location LocationMake:nTemp y:loc.y];
	}
	
	//上边
	nTemp = loc.y - 1;
	if(nTemp >= 0 && (num - 1) == [[[_mapLoc objectAtIndex:nTemp] objectAtIndex:loc.x] intValue])
	{
		return [Location LocationMake:loc.x y:nTemp];
	}
	
	
	//左边
	nTemp = loc.x - 1;
	if (nTemp >= 0 && (num - 1) == [[[_mapLoc objectAtIndex:loc.y] objectAtIndex:nTemp] intValue]) 
	{
		return [Location LocationMake:nTemp y:loc.y];
	}
	
	
	//下边
	nTemp = loc.y + 1;
	if(nTemp < _row && (num - 1) == [[[_mapLoc objectAtIndex:nTemp] objectAtIndex:loc.x] intValue])
	{
		return [Location LocationMake:loc.x y:nTemp];
	}
	
	return nil;
	
}


//武将相关地图操作

//标记武将背后地图数组
-(void) MarkWarriorMap:(Location*) loc
{
		
	if (0 == _type) 
	{
				
		[_openList removeAllObjects];
		[_closeList removeAllObjects];
		[_preOpenList removeAllObjects];
		[_openList addObject:loc];
		
		unsigned int i = 0;
		unsigned int j = 0;
		
		for (i = 0; i < _row; i++) 
		{
			for (j = 0; j < _column; j++) 
			{
				if ([[[_backWarriorMapLoc objectAtIndex:i] objectAtIndex:j] intValue] >=0 ) 
				{
					NSNumber *temp = [NSNumber numberWithInt:MAP_PATH];
					[[_backWarriorMapLoc objectAtIndex:i] replaceObjectAtIndex:j withObject:temp];
					
				}
			}
		}
		NSNumber *endtemp = [NSNumber numberWithInt:0];
		[[_backWarriorMapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:endtemp];

		
		while ([_openList count] > 0) 
		{
			[_preOpenList removeAllObjects];
			for(i = 0; i < [_openList count]; i++)
			{
				int nTemp = 0;
				Location *location = [_openList objectAtIndex:i];
				int nValue = [[[_backWarriorMapLoc objectAtIndex:location.y] objectAtIndex:location.x] intValue];
				
				//左边
				nTemp = location.x - 1;
				if(nTemp >= 0 && MAP_PATH == [[[_backWarriorMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
				{
					
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
					Location* aLoc = [Location LocationMake:nTemp y:location.y];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
				//右边
				nTemp = location.x + 1;
				if(nTemp < _column && MAP_PATH == [[[_backWarriorMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
					Location* aLoc = [Location LocationMake:nTemp y:location.y];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
				//上边
				nTemp = location.y - 1;
				if(nTemp >= 0 && MAP_PATH == [[[_backWarriorMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
					Location* aLoc = [Location LocationMake:location.x y:nTemp];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
				//下边
				nTemp = location.y + 1;
				if(nTemp < _row && MAP_PATH == [[[_backWarriorMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
					Location* aLoc = [Location LocationMake:location.x y:nTemp];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
			}
			
			[_openList removeAllObjects];
			for(Location* aLoc in _preOpenList)
			{
				[_openList addObject:aLoc];
			}
		}
		
		
//		for (i = 0; i < _row; i++) 
//		{
//			for (j = 0; j < _column ; j++) 
//			{
//				printf("%d\t", [[[_backWarriorMapLoc objectAtIndex:i] objectAtIndex:j] intValue]);
//			}
//			printf("\n");
//		}
//		printf("\n");
		return;
	}
	
	else if (1 == _type) 
	{
		
		[_openList removeAllObjects];
		[_closeList removeAllObjects];
		[_preOpenList removeAllObjects];
		[_openList addObject:loc];
		
		unsigned int i = 0;
		unsigned int j = 0;
		
		for (i = 0; i < _row; i++) 
		{
			for (j = 0; j < _column; j++) 
			{
				int nTemp = [[[_backWarriorMapLoc objectAtIndex:i] objectAtIndex:j] intValue];
				if (nTemp != MAP_NOTOWER && nTemp != MAP_WITHTOWER) 
				{
					NSNumber *temp = [NSNumber numberWithInt:MAP_DEFAULT];
					[[_backWarriorMapLoc objectAtIndex:i] replaceObjectAtIndex:j withObject:temp];
					
				}
			}
		}
		NSNumber *endtemp = [NSNumber numberWithInt:0];
		[[_backWarriorMapLoc objectAtIndex:loc.y] replaceObjectAtIndex:loc.x withObject:endtemp];
		
		
		while ([_openList count] > 0) 
		{
			[_preOpenList removeAllObjects];
			for(i = 0; i < [_openList count]; i++)
			{
				int nTemp = 0;
				Location *location = [_openList objectAtIndex:i];
				int nValue = [[[_backWarriorMapLoc objectAtIndex:location.y] objectAtIndex:location.x] intValue];
				
				//左边
				nTemp = location.x - 1;
				if(nTemp >= 0 && MAP_DEFAULT == [[[_backWarriorMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
				{
					
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
					Location* aLoc = [Location LocationMake:nTemp y:location.y];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
				//右边
				nTemp = location.x + 1;
				if(nTemp < _column && MAP_DEFAULT == [[[_backWarriorMapLoc objectAtIndex:location.y] objectAtIndex:(nTemp)] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:location.y] replaceObjectAtIndex:nTemp withObject:num];
					Location* aLoc = [Location LocationMake:nTemp y:location.y];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
				//上边
				nTemp = location.y - 1;
				if(nTemp >= 0 && MAP_DEFAULT == [[[_backWarriorMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
					Location* aLoc = [Location LocationMake:location.x y:nTemp];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
				//下边
				nTemp = location.y + 1;
				if(nTemp < _row && MAP_DEFAULT == [[[_backWarriorMapLoc objectAtIndex:nTemp] objectAtIndex:location.x] intValue])
				{
					NSNumber* num = [NSNumber numberWithInt:(nValue + 1)];
					[[_backWarriorMapLoc objectAtIndex:nTemp] replaceObjectAtIndex:location.x withObject:num];
					Location* aLoc = [Location LocationMake:location.x y:nTemp];
					[_preOpenList addObject:aLoc];
					[aLoc release];
					//continue;
				}
				
			}
			
			[_openList removeAllObjects];
			for(Location* aLoc in _preOpenList)
			{
				[_openList addObject:aLoc];
			}
		}
		
		
//		for (i = 0; i < _row; i++) 
//		{
//			for (j = 0; j < _column ; j++) 
//			{
//				printf("%d\t", [[[_backWarriorMapLoc objectAtIndex:i] objectAtIndex:j] intValue]);
//			}
//			printf("\n");
//		}
//		printf("\n");
		return;
	}
}

-(void) WarriorCopyBack
{
	
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < _row; i++)
	{
		for(j = 0; j < _column; j++)
		{
			int nTemp = 0;
			nTemp = [[[_backWarriorMapLoc objectAtIndex:i] objectAtIndex:j] intValue];
			NSNumber *num = [NSNumber numberWithInt:nTemp];
			
			[[_warriorMapLoc objectAtIndex:i] replaceObjectAtIndex:j withObject:num]; 
		}
	}
	
}

-(void) WarriorReset
{
	unsigned int i = 0;
	unsigned int j = 0;
	for(i = 0; i < _row; i++)
	{
		for(j = 0; j < _column; j++)
		{
			int nTemp = 0;
			nTemp = [[[_warriorMapLoc objectAtIndex:i] objectAtIndex:j] intValue];
			NSNumber *num = [NSNumber numberWithInt:nTemp];
			
			[[_backWarriorMapLoc objectAtIndex:i] replaceObjectAtIndex:j withObject:num]; 
		}
	}
}




//找到武将下一次移动方向
-(Location*) GetWarriorNextLocation:(Location *)loc
{
	int num = [[[_warriorMapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
	int nTemp = 0;
	
	//右边
	nTemp = loc.x + 1;
	if (((unsigned int)nTemp < _column && (num - 1)) == [[[_warriorMapLoc objectAtIndex:loc.y] objectAtIndex:nTemp] intValue]) 
	{
		return [Location LocationMake:nTemp y:loc.y];
	}
	
	//上边
	nTemp = loc.y - 1;
	if(nTemp >= 0 && (num - 1) == [[[_warriorMapLoc objectAtIndex:nTemp] objectAtIndex:loc.x] intValue])
	{
		return [Location LocationMake:loc.x y:nTemp];
	}
	
	
	//左边
	nTemp = loc.x - 1;
	if (nTemp >= 0 && (num - 1) == [[[_warriorMapLoc objectAtIndex:loc.y] objectAtIndex:nTemp] intValue]) 
	{
		return [Location LocationMake:nTemp y:loc.y];
	}
	
	
	//下边
	nTemp = loc.y + 1;
	if(nTemp < _row && (num - 1) == [[[_warriorMapLoc objectAtIndex:nTemp] objectAtIndex:loc.x] intValue])
	{
		return [Location LocationMake:loc.x y:nTemp];
	}
	
	NSException * myException = [NSException 
								 exceptionWithName:@"GetWarriroNextLoaction" 
								 reason:@"back nil" 
								 userInfo:nil];
	@throw myException;
	
	
	return nil;
}

//是否允许移动到这个地方
-(BOOL) EnableMoveToLoc:(Location *)loc fromWarriorLoc:(Location *) warriorLoc
{
	if (0 == _type) 
	{
		int temp = [[[_warriorMapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
		if (temp >=0 || MAP_PATH == temp) 
		{
			[self MarkWarriorMap:loc];
			[self WarriorCopyBack];
			return YES;
		}
		else 
		{
			return NO;
		}
	}
	else if(1 == _type)
	{
		int temp = [[[_warriorMapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
		if (temp >= 0 || MAP_DEFAULT == temp) 
		{
			[self MarkWarriorMap:loc];
			if ([[[_backWarriorMapLoc objectAtIndex:warriorLoc.y] objectAtIndex:warriorLoc.x] intValue] > 0) 
			{
				[self WarriorCopyBack];
				return YES;
			}
			
		}
		
		return NO;
	}
	return NO;
}

-(BOOL)IsEnableSkill:(Location*)loc
{
	if (0 == _type) 
	{
		int temp = [[[_warriorMapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
		if (temp >=0 || MAP_PATH == temp) 
		{
			return YES;
		}
		else 
		{
			return NO;
		}
	}
	else if(1 == _type)
	{
		int temp = [[[_warriorMapLoc objectAtIndex:loc.y] objectAtIndex:loc.x] intValue];
		if (temp >= 0 || MAP_DEFAULT == temp) 
		{
			return YES;
		}
		return NO;
	}
	return NO;
}


-(void )dealloc
{
	if(!_start)
	{
		[_start release];
	}
	if(!_end)
	{
		[_end release];
	}
	//地图
	[_mapLoc release];
	[_backMapLoc release];
	//武将
	[_warriorMapLoc release];
	[_backWarriorMapLoc release];
	//操作数组
	[_closeList release];
	[_preOpenList release];
	[_openList release];
	
	[super dealloc];
}

@end









































